package com.nbshop.action;


import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * BaseAction : Base
 * 
 * @author wangchuang
 * @version 1.0
 */
public class BaseAction extends ActionSupport implements ServletRequestAware,SessionAware{

	private static final long serialVersionUID = -824206167909234574L;

    private HttpServletRequest request;
  
    public void setServletRequest(HttpServletRequest request) { 
        this.request = request; 
    } 

    
    private Map session; 
    public void setSession(Map session) { 
        this. session = session; 
    } 
    
    public Map getSession() { 
       return  this. session ; 
    } 
    
	/**
	 * requesֵ
	 * 
	 * @param key
	 * @return String
	 */
	protected String getParamFromRequest(String key) {
		return request.getParameter(key);
	}
	
	
	
	/**
	 * requesֵ
	 * 
	 * @param key
	 * @return String
	 */
	protected String[] getParamArrFromRequest(String key) {
		return request.getParameterValues(key);
	}

	/**
	 * request
	 * 
	 * @param key
	 * @param value
	 */
	protected void setToRequest(String key, Object value) {
		request.setAttribute(key, value);
	}
    
	/**
	 *  从session huoqu
	 * 
	 * @param key

	 */
	protected Object getAttrFromSession(String key) {
		return session.get(key);
	}
	
	/**
	 * request
	 * 
	 * @param key
	 * @param value
	 */
	protected void setAttrToSession(String key,Object o) {
		session.put(key,o);
	}
	
	
	
	/**
	 * JSON
	 */
	public void sendMsg(String content) {
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			//response.setContentType("application/json");
			response.getWriter().write(content);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

    
	/**
	 * @param o
	 * @return
	 */
	protected String convertCollection2Json(Object o) {
		JSONArray json = new JSONArray();
		try {
			json = JSONArray.fromObject(o);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (null != json)
			return json.toString();
		return "";
	}
	
	
}
